//
//  SystemController.swift
//  foreign_swift
//
//  Created by 黄敦仁 on 15/4/22.
//  Copyright (c) 2015年 hdr. All rights reserved.
//

import UIKit

class SystemController:UIViewController , UITableViewDelegate , UITableViewDataSource {
    
    var dataSource:[Array<(String,String)>]!
    
    var tableView:UITableView!
    
    let cellIdentifier = "cell"
    
    init() {
        super.init(nibName: nil, bundle: nil)
        
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        initDataSource()
        
        self.title = "系统"
        
        addTableView()
    }
    
    func initDataSource(){
        
        var arr1 = [(String,String)]()
        arr1.append(("个性化","system_individuation.png"))
        arr1.append(("语音","system_language.png"))
        arr1.append(("HealthKit设置","system_healthKit.png"))
        
        var arr2 = [(String,String)]()
        arr2.append(("反馈","system_feedback.png"))
        arr2.append(("我的设备","system_mydevice.png"))
        
        var arr3 = [(String,String)]()
        arr3.append(("小宝助手","system_introduce.png"))
        arr3.append(("关于","system_about.png"))
        
        var arr4 = [(String,String)]()
        arr4.append(("称重提醒","system_remind.png"))
        arr4.append(("设备密码","system_password.png"))
        
        dataSource = [arr1,arr2,arr3,arr4]
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func addTableView(){
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height),style:UITableViewStyle.Grouped)
        
        tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
        
        tableView.sectionHeaderHeight = 0
        
        tableView.sectionFooterHeight = 20
        tableView.showsVerticalScrollIndicator = false
        
        self.view.addSubview(tableView)
        
        tableView.delegate = self
        
        tableView.dataSource = self
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource[section].count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return dataSource.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as? UITableViewCell
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: self.cellIdentifier)
            cell?.selectionStyle = UITableViewCellSelectionStyle.None
            cell?.separatorInset = UIEdgeInsetsZero
        }
        
        let section = indexPath.section,row = indexPath.row
        
        let (title,imageName) = dataSource[section][row]
        
        switch((section,row)){
        case (0,0) , (0,2):
            cell?.imageView?.image = UIImage(named: imageName)
        default:
            cell?.imageView?.image = UIImage(named: imageName)

        }
        
        cell?.textLabel?.text = title
        cell?.textLabel?.font = UIFont.systemFontOfSize(17)
        cell?.accessoryView = ViewHelper.initImageViewWithImageName("system_arrow.png")
        
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println("点击了（\(dataSource[indexPath.section][indexPath.row])）")
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
